#include "devicelist.h"
#include <QDebug>

DeviceList::DeviceList(QObject *parent) :
    QAbstractListModel(parent)
{
    m_display_list.clear();
    m_roleNames.insert(RoleName, "name");
    m_roleNames.insert(RolePortname, "portName");
    m_roleNames.insert(RoleProtocol, "protocol");
    m_roleNames.insert(RoleCommaddr, "commAddr");
    m_roleNames.insert(RolecolorCode, "colorCode");
    m_roleNames.insert(RoleSystemTime, "systemTime");
    m_roleNames.insert(RoledataString, "dataString");





    //从数据库初始化设备参数
//    m_display_list.push_back(Device("sensor01","COM10","A",1));
//    m_display_list.push_back(Device("sensor02","COM10","A",2));
//    m_display_list.push_back(Device("sensor03","COM10","A",3));
//    m_display_list.push_back(Device("sensor04","COM10","A",4));
//    m_display_list.push_back(Device("sensor05","COM10","A",5));
//    m_display_list.push_back(Device("sensor06","COM10","A",6));
//    m_display_list.push_back(Device("sensor07","COM10","A",7));

}


int DeviceList::rowCount(const QModelIndex &parent) const
{
    return m_display_list.size();
}

int DeviceList::rowCountRead()
{
    return m_display_list.size();
}


QVariant DeviceList::data(const QModelIndex &index, int role) const
{
    Device ele = m_display_list.at(index.row());
    QVariant var;
    switch (role) {
//    RoleName
//    RolePortname,
//    RoleProtocol,
//    RoleCommaddr,
//    RolecolorCode
    case RoleName:
        var = ele.name;
        break;
    case RolePortname:
        var = ele.portName;
        break;
    case RoleProtocol:
        var = ele.protocol;
        break;
    case RoleCommaddr:
        var = ele.comAddr;
        break;
    case RolecolorCode:
        var = ele.colorCode;
        break;
    case RoleSystemTime:
        var = ele.SystemTime;
        break;
    case RoledataString:
        var = ele.dataString;
        break;
    default:
        break;
    }

    return var;
}


QHash<int, QByteArray> DeviceList::roleNames() const
{
    return m_roleNames;
}

void DeviceList::changeName(int index)
{
    QString timeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    beginResetModel();
    m_display_list[index].dataString="瞬时流量：1000 m3 , 累积流量5000 m3   @ "+timeStr;
    endResetModel();

//    if(index == 1){
//        Device ele1("change01","COM10","A",1);
//        m_display_list[index] = ele1;
//        qDebug()<<"changeName(1)"<<m_display_list[index].name;
//    }

//    if(index == 2){
//        Device ele2("change02","COM10","A",1);
//        m_display_list[index] = ele2;
//        qDebug()<<"changeName(2)"<<m_display_list[index].name;
//    }

    //emit dataChanged();
}

void DeviceList::clickItem(int index)
{
    //取出设备端口，取出设备协议，取工作任务

    //打开设备端口，根据工作任务调用协议数据生成方法指令数据，从端口发送指令

    //等待取回端口数据，根据工作任务调用协议数据分析方法返回所需要的数据

    //更新界面数据显示字串
    QString timeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    beginResetModel();
    m_display_list[index].dataString="瞬时流量：1000 m3 , 累积流量5000 m3   @ "+timeStr;
    endResetModel();
}

void DeviceList::changeDataString(int index,QString str)
{
    //取出设备端口，取出设备协议，取工作任务

    //打开设备端口，根据工作任务调用协议数据生成方法指令数据，从端口发送指令

    //等待取回端口数据，根据工作任务调用协议数据分析方法返回所需要的数据

    //更新界面数据显示字串
    beginResetModel();
    m_display_list[index].dataString=str;
    endResetModel();
}


void DeviceList::addDevice(QString name_, QString portname_, QString protocol_, QString addr_)
{
    beginInsertRows(QModelIndex(), m_display_list.count(), m_display_list.count());
    m_display_list.push_back(Device(name_,portname_,protocol_,addr_.toInt()));
    endInsertRows();
    qDebug()<<"addDevice"<<m_display_list.size();
    //emit dataChanged();
}
